﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Dropfeupload
{
    public partial class Configure : Form
    {
        FeuploadCredentials myFlc;
        Feupload myFl;
        public Configure()
        {
            InitializeComponent();
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            try
            {
                myFlc = new FeuploadCredentials(tbUser.Text, tbPw.Text);
                myFl = new Feupload(myFlc);
                myFl.login();
            }
            catch (CredentialNotValid)
            {
                MessageBox.Show("Credentials are incorrectly inserted.");
                return;
            }
            catch (AuthenticationFailure)
            {
                tbPw.Text = "";
                MessageBox.Show("Credentials are incorrect. Make sure you correctly typed your username and password.");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                return;
            }
            btLogin.Enabled = false;
			btFileList_Click(null, null);
        }

        private void btFileList_Click(object sender, EventArgs e)
        {
            if (btLogin.Enabled)
                return;

            myFl.getFileList();
            ltbFiles.Items.Clear();

            foreach (FeuploadFile flf in myFl.flFiles)
            {
                ltbFiles.Items.Add(flf.fileName + " (" + flf.fileId + ")");
            }
        }

        private void btDownloadFile_Click(object sender, EventArgs e)
        {
            if (ltbFiles.SelectedIndex < 0)
                return;

            myFl.flFiles[ltbFiles.SelectedIndex].downloadFile("", myFl.flAuth);
        }

        private void btRemoveFile_Click(object sender, EventArgs e)
        {
            if (ltbFiles.SelectedIndex < 0)
                return;

            myFl.flFiles[ltbFiles.SelectedIndex].removeFile(myFl.flAuth);
            btFileList_Click(null, null);
        }

        private void btUploadFile_Click(object sender, EventArgs e)
        {
            myFl.uploadFile();
        }
    }
}